package com.topisv.tms.web.express;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.ExpressStyle;
import com.topisv.tms.entity.LogisticsCompany;
import com.topisv.tms.entity.PackformStyle;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.ExpressStyleService;
import com.topisv.tms.service.LogisticsCompanyService;
import com.topisv.tms.service.PackformStyleService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.utils.ExpressRule;
import com.topisv.tms.utils.FileOperation;
import com.topisv.tms.utils.UploadImageUtil;
import com.topisv.tms.utils.enums.StatusEnums;
import com.topisv.tms.web.stock.outstorage.OutstorageBillController;

/**
 * 快递打印模版管理Controller
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping(value = "/v2/expressPrinting")
public class ExpressPrinting {
	private static final Logger logger = LoggerFactory.getLogger(ExpressPrinting.class);
	@Autowired
	private LogisticsCompanyService logisticsCompanyService;
	@Autowired
	private ExpressStyleService expressStyleService;
	@Autowired
	private PackformStyleService packformStyleService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private AreaGroupService areaGroupService;
	@RequestMapping(value = "main")
	public void main(){
		List<AreaGroup> areaGroups=areaGroupService.findAll();
		Iterable<ExpressStyle> expressStyles=expressStyleService.findAll();
		for(AreaGroup a:areaGroups){
			for(ExpressStyle e:expressStyles){
				ExpressStyle es=new ExpressStyle();
				es.setAreaGroupCode(a.getGroupCode());
				es.setCustomerCode(e.getCustomerCode());
				es.setCustomerName(e.getCustomerName());
				es.setLogisticsCompany(e.getLogisticsCompany());
				es.setRemarks(e.getRemarks());
				es.setStyleDefault(e.getStyleDefault());
				es.setStyleImageUrl(e.getStyleImageUrl());
				es.setStyleName(e.getStyleName());
				es.setStyleContent(e.getStyleContent());
				expressStyleService.saveExpressStyle(es);
			}
		}
	}
	
	/**
	 * 根据物流公司进入相应公司的快递单展示页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "viewExpressStyle/{id}")
	public String viewExpressStyle(@PathVariable("id") Long id, Model model,RedirectAttributes redirectAttributes) {
		LogisticsCompany logisticsCompany = logisticsCompanyService.getLogisticsCompany(id);
		List<LogisticsCompany> logisticsCompanys = logisticsCompanyService.findAll();
		model.addAttribute("logisticsCompanys", logisticsCompanys);
		model.addAttribute("logisticsCompanyItem", logisticsCompany);
		redirectAttributes.addFlashAttribute("message", "删除成功");
		return "expressPrinting/viewExpressStyle";
	}
	
	@RequestMapping(value = "updataExpressStyle/{id}")
	public String updataExpressStyle(@PathVariable("id") Long id, Model model) {
		ExpressStyle expressStyle = expressStyleService.findOne(id);
		model.addAttribute("expressStyle", expressStyle);
		model.addAttribute("titlePage", "修改快递单");
		model.addAttribute("status", "1");
		model.addAttribute("cainiaoFlg", expressStyle.getCainiaoFlg());
		if("1".equals(expressStyle.getCainiaoFlg())){
			return "expressPrinting/updateExpressStyleCN";
		}else if("mail".equals(expressStyle.getType())){
			return "expressPrinting/updateMailExpress";
		}else if("normal".equals(expressStyle.getType())){
			return "expressPrinting/updateExpressStyle";
		}
		return "expressPrinting/updateExpressStyle";

	}


	@RequestMapping(value = "deleteExpress/{id}")
	public String deleteExpress(@PathVariable("id") Long id) {
		ExpressStyle expressStyle = expressStyleService.findOne(id);
		expressStyleService.deleteExpressStyleId(id);
		return "redirect:/v2/expressPrinting/init";
	}
	
	@RequestMapping(value = "setExpressStyle",method=RequestMethod.GET)
	public void setExpressStyle(HttpServletRequest request,HttpServletResponse response){
		JSONObject json=null;
		PrintWriter out=null;
		String path=request.getRealPath("");
		try {
			json=new JSONObject();
			out=response.getWriter();
			Iterable<ExpressStyle> expressStyles=expressStyleService.findAll();
			for(ExpressStyle e:expressStyles){
				String content=FileOperation.reader(path, e.getStyleContent());
				e.setStyleContent(content);
				expressStyleService.saveExpressStyle(e);
			}
			Iterable<PackformStyle>  packformStyles=packformStyleService.findAll();
			for(PackformStyle p:packformStyles){
				String content=FileOperation.reader(path, p.getStyleContent());
				p.setStyleContent(content);
				packformStyleService.savePackformStyle(p);
			}
			json.put("status", "1");
			json.put("content", "修改成功");
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content",e.getMessage());
			out.print(json.toString());
		}
	}

	@RequestMapping(value = "init")
	public String viewExpressStyle(Model model) {
		ShiroUser user=getShiroUser();
		List<AreaGroup> areaGroups=null;
		if(StatusEnums.user_type_customer.getStatus().equals(user.userType)){
			 areaGroups=this.areaGroupService.loadAreaGroupByAuthority(user, user.customerCode);
		}else if(StatusEnums.user_type_paiu.getStatus().equals(user.userType)){
			 areaGroups=this.areaGroupService.loadByWarehouseType("paiu");
		}
		List<LogisticsCompany> logisticsCompanys = logisticsCompanyService.findAll();
		model.addAttribute("logisticsCompanys", logisticsCompanys);
		model.addAttribute("areaGroups", areaGroups);
		//查出当前用户关联的商家列表
		ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		List<Customers> customerList=this.customersService.loadCustomerByAuthority(curUser);
		model.addAttribute("customerList",customerList);
		return "expressPrinting/viewExpressStyle";
	}

	@RequestMapping(value = "")
	public String getOrders(
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "function", defaultValue = "0") int function,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			Model model, ServletRequest request) {
		List<LogisticsCompany> logisticsCompanys = logisticsCompanyService.findAll();
		model.addAttribute("logisticsCompanys", logisticsCompanys);
		model.addAttribute("action", "create");
		model.addAttribute("model", "model");
		model.addAttribute("packformStyle", "packformStyle");
		return "expressPrinting/expressPrintingList";
	}

	@RequestMapping(value = "ajaxStyleName")
	public void ajaxStyleName(
			@RequestParam(value = "logisticsCompanyId") Long logisticsCompanId,
			HttpServletResponse response) {
		JSONObject json=null;
		PrintWriter out=null;
		try {
			json=new JSONObject();
			out=response.getWriter();
			List<ExpressStyle> expressStyle = expressStyleService.findStyleNames(logisticsCompanId);
			if (expressStyle.isEmpty()) {
				json.put("status", "-1");
				json.put("content", "没有该快递公司的模板信息");
				out.print(json.toString());
				return;
			}
			json.put("status", "1");
			json.put("content", expressStyle);
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}
	
	/**
	 * 
	 * @param id 快递单ID
	 * @param type 快递单类型
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addExpress/{id}/{type}/{cainiaoFlg}")
	public String addExpress(@PathVariable("id") Long id,@PathVariable("type") String type,@PathVariable("cainiaoFlg") String cainiaoFlg, Model model) {
		ShiroUser user = getShiroUser();
		List<AreaGroup> areaGroups=null;
		try {
			LogisticsCompany logisticsCompany = logisticsCompanyService.getLogisticsCompany(id);
			List<Customers> customers=customersService.loadCustomerByAuthority(user);
			if(StatusEnums.user_type_paiu.getStatus().equals(user.userType)){
				areaGroups=areaGroupService.findAll();
			}
			model.addAttribute("logisticsCompany", logisticsCompany);
			model.addAttribute("areaGroups", areaGroups);
			model.addAttribute("customers", customers);
			model.addAttribute("titlePage", "增加快递单");
			model.addAttribute("status", "0");
			model.addAttribute("cainiaoFlg", cainiaoFlg);
			if ("1".equals(cainiaoFlg)) {
				return "expressPrinting/addExpressStyleCN";
			}else if("mail".equals(type)){
				return "expressPrinting/addMailExpressStyle";
			}else if("normal".equals(type)){
				return "expressPrinting/addExpressStyle";
			}
			
		} catch (Exception e) {
			logger.error("", e);
		}
		return "expressPrinting/addExpress/"+id;

	}
	
	@RequestMapping(value = "updateAjaxMailExpressStyle", method = RequestMethod.POST)
	public void updateAjaxMailExpressStyle(
			@RequestParam(value = "expressStyleName") String expressStyleName,
			@RequestParam(value = "content") String content,
			@RequestParam(value = "type") String type,
			@RequestParam(value = "expressWidth") String expressWidth,
			@RequestParam(value = "expressHeight") String expressHeight,
			@RequestParam(value = "id") Long id,
			HttpServletRequest request,
			HttpServletResponse response) {
		JSONObject json=null;
		PrintWriter out=null;
		try {
			json=new JSONObject();
			out=response.getWriter();
			if (expressWidth != null) {
				boolean fa = ExpressRule.checkNumber(expressWidth);
				if (!fa) {
					json.put("status", "-1");
					json.put("content", "宽度错误");
					out.print(json.toString());
					return;
				}
			} else {
				json.put("status", "-1");
				json.put("content", "宽度错误");
				out.print(json.toString());
				return;
			}
			if (expressHeight != null) {
				boolean fa = ExpressRule.checkNumber(expressHeight);
				if (!fa) {
					json.put("status", "-1");
					json.put("content", "高度错误");
					out.print(json.toString());
					return;
				}
			} else {
				json.put("status", "-1");
				json.put("content", "高度错误");
				out.print(json.toString());
				return;
			}

			ExpressStyle expressStyle= expressStyleService.findOne(id);
			if(expressStyle==null){
				json.put("status", "-1");
				json.put("content", "该模板不存在");
				out.print(json.toString());
				return;
			}
			content=URLDecoder.decode(content, "utf-8");
			expressStyle.setStyleName(expressStyleName);
			expressStyle.setStyleContent(content);
			expressStyle.setStyleImageUrl("");
			expressStyle.setRemarks(expressWidth + "*" + expressHeight);
			expressStyle.setType(type);
			expressStyleService.saveExpressStyle(expressStyle);
			json.put("status", "1");
			json.put("content", "更新成功");
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}
	
	@RequestMapping(value = "addAjaxMailExpressStyle", method = RequestMethod.POST)
	public void addAjaxMailExpressStyle(
			@RequestParam(value = "logisticsCompanyId") Long logisticsCompanyId,
			@RequestParam(value = "expressStyleName") String expressStyleName,
			@RequestParam(value = "content") String content,
			@RequestParam(value = "type") String type,
			@RequestParam(value = "expressWidth") String expressWidth,
			@RequestParam(value = "expressHeight") String expressHeight,
			@RequestParam(value = "customerCode" ,required=false) String customerCode,
			@RequestParam(value = "areaGroupCode") String areaGroupCode,
			@RequestParam(value = "cainiaoFlg" ,required=false,defaultValue="0") String cainiaoFlg,
			@RequestParam(value = "defineParam",required=false) String defineParam,
			HttpServletRequest request,
			HttpServletResponse response) {
		JSONObject json=null;
		PrintWriter out=null;
		LogisticsCompany logisticsCompany = null;
		int defaultExpressId=0;
		try {
			json=new JSONObject();
			out=response.getWriter();
			if (expressWidth != null) {
				boolean fa = ExpressRule.checkNumber(expressWidth);
				if (!fa) {
					json.put("status", "-1");
					json.put("content", "宽度错误");
					out.print(json.toString());
					return;
				}
			} else {
				json.put("status", "-1");
				json.put("content", "宽度错误");
				out.print(json.toString());
				return;
			}
			if (expressHeight != null) {
				boolean fa = ExpressRule.checkNumber(expressHeight);
				if (!fa) {
					json.put("status", "-1");
					json.put("content", "高度错误");
					out.print(json.toString());
					return;
				}
			} else {
				json.put("status", "-1");
				json.put("content", "高度错误");
				out.print(json.toString());
				return;
			}

			logisticsCompany = logisticsCompanyService.getLogisticsCompany(logisticsCompanyId);
			if(logisticsCompany==null){
				json.put("status", "-1");
				json.put("content", "该快递公司不存在");
				out.print(json.toString());
				return;
			}
			
			ExpressStyle expressStyle = new ExpressStyle();
			if("1".equals(cainiaoFlg)){
				expressStyle.setStyleContent(content);
			}else{
				content=URLDecoder.decode(content, "utf-8");
				expressStyle.setStyleContent(content.replace("\\n", ""));
			}
			expressStyle.setCustomerCode(customerCode);
			expressStyle.setLogisticsCompany(logisticsCompany);
			expressStyle.setStyleName(expressStyleName);
			expressStyle.setStyleImageUrl("");
			expressStyle.setRemarks(expressWidth + "*" + expressHeight);
			expressStyle.setType(type);
			expressStyle.setAreaGroupCode(areaGroupCode);
			expressStyle.setStyleDefault(defaultExpressId);
			expressStyle.setCainiaoFlg(cainiaoFlg);
			expressStyle.setDefineParam(defineParam);
			expressStyleService.saveExpressStyle(expressStyle);
			json.put("status", "1");
			json.put("content", "增加成功");
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}


	/**
	 * 增加快递单样式
	 * @param logisticsCompanyId
	 * @param expressStyleName
	 * @param content
	 * @param checkDefault
	 * @param imageSrcName
	 * @param expressWidth
	 * @param expressHeight
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "addAjaxExpressStyle", method = RequestMethod.POST)
	public void addAjaxExpressStyle(
			@RequestParam(value = "logisticsCompanyId") Long logisticsCompanyId,
			@RequestParam(value = "expressStyleName") String expressStyleName,
			@RequestParam(value = "content") String content,
			@RequestParam(value = "type") String type,
			@RequestParam(value = "imageSrcName") String imageSrcName,
			@RequestParam(value = "expressWidth") String expressWidth,
			@RequestParam(value = "expressHeight") String expressHeight,
			@RequestParam(value = "customerCode") String customerCode,
			@RequestParam(value = "areaGroupCode") String areaGroupCode,
			@RequestParam(value = "cainiaoFlg") String cainiaoFlg,
			HttpServletRequest request,
			HttpServletResponse response) {
		JSONObject json=null;
		PrintWriter out=null;
		LogisticsCompany logisticsCompany = null;
		int defaultExpressId=0;
		try {
			json=new JSONObject();
			out=response.getWriter();
			
			if (imageSrcName != null && !imageSrcName.trim().equals("")) {
				imageSrcName = imageSrcName.substring(imageSrcName.lastIndexOf("/") + 1);
			} else {
				json.put("status", "-1");
				json.put("content", "图片名称错误");
				out.print(json.toString());
				return;
			}

			if (expressWidth != null) {
				boolean fa = ExpressRule.checkNumber(expressWidth);
				if (!fa) {
					json.put("status", "-1");
					json.put("content", "宽度错误");
					out.print(json.toString());
					return;
				}
			} else {
				json.put("status", "-1");
				json.put("content", "宽度错误");
				out.print(json.toString());
				return;
			}
			if (expressHeight != null) {
				boolean fa = ExpressRule.checkNumber(expressHeight);
				if (!fa) {
					json.put("status", "-1");
					json.put("content", "高度错误");
					out.print(json.toString());
					return;
				}
			} else {
				json.put("status", "-1");
				json.put("content", "高度错误");
				out.print(json.toString());
				return;
			}

			logisticsCompany = logisticsCompanyService.getLogisticsCompany(logisticsCompanyId);
			if(logisticsCompany==null){
				json.put("status", "-1");
				json.put("content", "该快递公司不存在");
				out.print(json.toString());
				return;
			}
			ExpressStyle expressStyle = new ExpressStyle();
			expressStyle.setCustomerCode(customerCode);
			expressStyle.setLogisticsCompany(logisticsCompany);
			expressStyle.setStyleName(expressStyleName);
			expressStyle.setStyleContent(content);
			expressStyle.setStyleImageUrl(imageSrcName);
			expressStyle.setRemarks(expressWidth + "*" + expressHeight);
			expressStyle.setType(type);
			expressStyle.setAreaGroupCode(areaGroupCode);
			expressStyle.setStyleDefault(defaultExpressId);
			expressStyle.setCainiaoFlg(cainiaoFlg);
			expressStyleService.saveExpressStyle(expressStyle);
			json.put("status", "1");
			json.put("content", "增加成功");
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 更新快递单样式
	 * @param expressStyleName
	 * @param content
	 * @param checkDefault
	 * @param imageSrcName
	 * @param expressWidth
	 * @param expressHeight
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "updateAjaxExpressStyle")
	public void updateAjaxExpressStyle(
			@RequestParam(value = "expressStyleName") String expressStyleName,
			@RequestParam(value = "content") String content,
			@RequestParam(value = "type") String type,
			@RequestParam(value = "expressStyleId") Long expressStyleId,
			@RequestParam(value = "expressWidth") String expressWidth,
			@RequestParam(value = "expressHeight") String expressHeight,
			@RequestParam(value = "defineParam",required=false) String defineParam,
			HttpServletRequest request,
			HttpServletResponse response) {
		
		ExpressStyle expressStyle = null;
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			
			expressStyle = expressStyleService.findOne(expressStyleId);
			if(expressStyle==null){
				json.put("status", "-1");
				json.put("content", "该样式不存在");
				out.print(json.toString());
				return;
			}
			//content=URLDecoder.decode(content, "utf-8");
			expressStyle.setStyleName(expressStyleName);
			expressStyle.setType(type);
			expressStyle.setDefineParam(defineParam);
			expressStyle.setRemarks(expressWidth + "*" + expressHeight);
			expressStyle.setStyleContent(content);
			expressStyleService.saveExpressStyle(expressStyle);
			json.put("status", "1");
			json.put("content", "修改成功");
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}

	}

	@RequestMapping(value = "selectAjaxDesign")
	public void selectAjaxDesign(
			@RequestParam(value = "expressStyleId") Long expressStyleId,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			
			ExpressStyle expressStyle = expressStyleService.findOne(expressStyleId);
			if(expressStyle==null){
				json.put("status", "-1");
				json.put("content", "该样式不存在");
				out.print(json.toString());
				return;
			}
			String[] params = getParams(expressStyle.getRemarks());
			String content = FileOperation.reader(expressStyle);
			json.put("status", "1");
			json.put("width", params[0]);
			json.put("height", params[1]);
			json.put("content", content);
			json.put("name", expressStyle.getStyleName());
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	private String[] getParams(String content) {
		return content.split("\\*");
	}

	public boolean isBlank(String val){
		return null == val || val.trim().length()==0;
	}
	
	/**
	 * libin 新增刪除模板功能
	 * @param id
	 * @param logisticsCompanyId
	 * @param areaGroupCode
	 * @param cainiaoFlg
	 * @param customerCode
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "deleteExpressNew/{id}",method = RequestMethod.POST)
	public void deleteExpressNew(@PathVariable("id") Long id,@RequestParam(value = "logisticsCompanys") Long logisticsCompanyId,
			@RequestParam(value = "areaGroupCode") String areaGroupCode,
			@RequestParam(value = "cainiaoFlg") String cainiaoFlg,
			@RequestParam(value = "customerCode",required=false) String customerCode,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		String basePath=null;
		if(isBlank(customerCode))customerCode = null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			expressStyleService.deleteExpressStyleId(id);
			basePath = FileOperation.getPath();
			List<ExpressStyle> expressStyles = expressStyleService.findByLogisticsCompanyAndAreaGroupCode(logisticsCompanyId, areaGroupCode,cainiaoFlg,customerCode);
			if (!expressStyles.isEmpty()) {
				for (int i = 0; i < expressStyles.size(); i++) {
					ExpressStyle expressStyle = expressStyles.get(i);
					expressStyle.setStyleImageUrl(FileOperation.getExpressStyleImage(basePath, expressStyle.getStyleImageUrl()));
					customerCode = expressStyles.get(i).getCustomerCode();
					if (!StringUtils.isBlank(customerCode)) {
						Customers customer = customersService.findByCustomerCode(customerCode);
						expressStyles.get(i).setCustomerName(customer.getCustomerName());
					}
				}
				json.put("status", "1");
				json.put("content", expressStyles);
				out.println(json.toString());
			} else {
				json.put("status", "-1");
				json.put("content", "该快递公司不存在模板");
				out.print(json.toString());
			}
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}
	
	@RequestMapping(value = "companyAjaxExpressStyle", method = RequestMethod.POST)
	public void companyAjaxExpressStyle(
			@RequestParam(value = "logisticsCompanys") Long logisticsCompanyId,
			@RequestParam(value = "areaGroupCode") String areaGroupCode,
			@RequestParam(value = "cainiaoFlg") String cainiaoFlg,
			@RequestParam(value = "customerCode",required=false) String customerCode,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		String basePath=null;
		if(isBlank(customerCode))customerCode = null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			
			basePath = FileOperation.getPath();
			List<ExpressStyle> expressStyles = expressStyleService.findByLogisticsCompanyAndAreaGroupCode(logisticsCompanyId, areaGroupCode,cainiaoFlg,customerCode);
			if (!expressStyles.isEmpty()) {
				for (int i = 0; i < expressStyles.size(); i++) {
					ExpressStyle expressStyle = expressStyles.get(i);
					expressStyle.setStyleImageUrl(FileOperation.getExpressStyleImage(basePath, expressStyle.getStyleImageUrl()));
					customerCode = expressStyles.get(i).getCustomerCode();
					if (!StringUtils.isBlank(customerCode)) {
						Customers customer = customersService.findByCustomerCode(customerCode);
						expressStyles.get(i).setCustomerName(customer.getCustomerName());
					}
				}
				json.put("status", "1");
				json.put("content", expressStyles);
				out.println(json.toString());
			} else {
				json.put("status", "-1");
				json.put("content", "该快递公司不存在模板");
				out.print(json.toString());
			}
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}
	
	@RequestMapping(value = "getAjaxExpress")
	public void getAjaxExpress(HttpServletResponse response,
			HttpServletRequest request) {
		String expressIds = request.getParameter("expressStylesId");
		PrintWriter out = null;
		Long expressId = 0l;
		JSONObject json =null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			if (expressIds == null||"".equals(expressIds.trim())||"null".equals(expressIds)) {
				json.put("status", "0");
				json.put("content", "id不正确");
				out.print(json.toString());
				return;
			}
			expressId = Long.parseLong(expressIds);
			ExpressStyle expressStyle = expressStyleService.findOne(expressId);
			
			json.put("status", "1");
			json.put("content", expressStyle);
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}
	
	@RequestMapping(value = "ajaxGetAllExpress")
	public void ajaxGetAllExpress(@RequestParam(value="customerCode") String customerCode,
			@RequestParam(value="areaGroupCode") String areaGroupCode,
			@RequestParam(value="type") String type,HttpServletResponse response) {
		
		PrintWriter out = null;
		JSONObject json =null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			List<ExpressStyle> es=new ArrayList<ExpressStyle>();
			List<ExpressStyle> expressStyles=this.expressStyleService.findByCustomerCodeAndAreaGroupCode(customerCode, areaGroupCode);
			
				for(ExpressStyle e:expressStyles){
					if(type.equals(e.getType())){
						es.add(e);
					}
				}
			
			json.put("status", "1");
			json.put("content", es);
			out.print(json.toString());
		} catch (IOException e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 取消默认
	 * @param expressStyleId
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "cancelDefault")
	public void cancelDefault(
			@RequestParam(value = "expressStyleId") Long expressStyleId,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			
			expressStyleService.deleteStyleDufault(expressStyleId);
			json.put("status", "1");
			json.put("content", "删除成功");
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	@RequestMapping(value = "addDefault")
	public void addDefault(@RequestParam(value = "expressStyleId") Long expressStyleId,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			ExpressStyle expressStyle = expressStyleService.findOne(expressStyleId);
			ExpressStyle tempExpressStyle = expressStyleService.findStyleDefault(expressStyle.getLogisticsCompany().getId(),expressStyle.getCustomerCode());
			if (tempExpressStyle != null) {
				json.put("status", "-1");
				json.put("content", "设置失败");
				out.print(json.toString());
				return;
			}
			expressStyle.setStyleDefault(1);
			expressStyleService.saveExpressStyle(expressStyle);
			json.put("status", "1");
			json.put("content", "设置成功");
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String createExpressPrinting(
			@RequestParam(value = "logisticsCompanyId") Long logisticsCompanId,
			@RequestParam(value = "nameBz") String nameBz,
			@RequestParam(value = "name") List<String> names,
			@RequestParam(value = "styleId") Long styleId,
			RedirectAttributes redirectAttributes) {
		ExpressStyle expressStyle = expressStyleService.findOne(styleId);
		expressStyle.setRemarks(nameBz);
		expressStyleService.saveExpressStyle(expressStyle);

		return "redirect:/v2/expressPrinting/";
	}
	
	@RequestMapping(value = "ajaxExpressStyle", method = RequestMethod.POST)
	public void ajaxExpressStyle(
			@RequestParam(value = "companyCode") String companyCode,
			@RequestParam(value = "areaGroupCode") String areaGroupCode,
			@RequestParam(value = "customerCode") String customerCode,
			@RequestParam(value = "type") String type,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json = new JSONObject();
			 List<ExpressStyle> expressStyles = this.expressStyleService.findStyleNew(companyCode, customerCode, areaGroupCode, type);
			json.put("status", "1");
			json.put("content", expressStyles);
			out.print(json.toString());
		} catch (Exception e) {
			logger.error("", e);
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
		}
	}

	/**
	 * 图像上传
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "addImage")
	public void addImage(HttpServletRequest request,
			HttpServletResponse response) {
		UploadImageUtil.upload(request, response);
	}
	
	private ShiroUser getShiroUser() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user;
	}
}
